<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Branches extends BIT_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct()
	{
		parent::__construct();
		$this->header_views = array('admin/header');
		$this->footer_views = array('admin/footer');
	
		
		// Check if user is loggedin
		if(!$this->isloggedin())
		{
			redirect('admin/login');
		}
	}
	private function isloggedin(){return (isset($this->session->userdata) && $this->session->userdata('loggedin')===TRUE);}

	public function index()
	{

		$branches = array();
		$mainb = $this->db->get_where('branches', array('parent'=>0))->result();
		foreach($mainb as $b){
			$b->children = array();
			foreach($this->db->get_where('branches', array('parent'=>$b->id))->result() as $s){
				array_push($b->children, $s);
			}
			array_push($branches, $b);
		}
		$this->view_data['branches'] = $branches;

		$this->v('admin/branches');

	}

	public function remove($branchid){

		$removed = $this->db->delete('branches',array('id' => $branchid));
		$this->view_message['message'] = "Borttagen!";
		$this->index();
	}

	// Only the form for adding, handling adds is in other files
	public function add(){

		if($this->input->post('mbranch')) $this->add_main();
		else if($this->input->post('sbranch')) $this->add_sub();

		else $this->add_start();
	}

	public function add_start(){
		$this->view_data['mbs'] = $this->db->get_where('branches', array('parent'=>0))->result();
		$this->v('admin/add_branch');
	}

	// Add a new main-branch
	public function add_main(){

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$form_rules = array(
			array(
				'field' => 'mbranch',
				'label' => 'namn',
				'rules' => 'trim|required|min_length[3]'
			)
		);

		$this->form_validation->set_message('required','Du måste fylla i ett namn');
		$this->form_validation->set_message('min_length','Ange minst 3 tecken');

		$this->form_validation->set_rules($form_rules);
		
		if($this->form_validation->run() == TRUE) {
			// Set message and new email
			$this->view_data['message'] = $this->input->post('mbranch') . " lades till!";
			$data = array(
				'name'		=> $this->input->post('mbranch'),
				'parent'	=> 0
			);
			$this->db->insert('branches', $data);

			$this->add_start();
		}
	}

	// Add a new main-branch
	public function add_sub(){

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$form_rules = array(
			array(
				'field' => 'sbranch',
				'label' => 'namn',
				'rules' => 'trim|required|min_length[3]'
			)
		);

		$this->form_validation->set_message('required','Du måste fylla i ett namn');
		$this->form_validation->set_message('min_length','Ange minst 3 tecken');

		$this->form_validation->set_rules($form_rules);
		
		if($this->form_validation->run() == TRUE) {
			// Set message and new email
			$this->view_data['message'] = $this->input->post('sbranch') . " lades till!";
			$data = array(
				'name'		=> $this->input->post('sbranch'),
				'parent'	=> $this->input->post('main_branch')
			);
			$this->db->insert('branches', $data);

			$this->add_start();
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */